package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author majun
 * @package sky-take-out
 * @date 2024/6/6 : 下午1:36
 */

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询今日运营数据
     *
     * @return
     */
    public BusinessDataVO businessData(String format) {

        //今日营业额
        Map map = new HashMap<>();
        map.put("beginTime", format);
        map.put("status", Orders.COMPLETED);

        Double turnover = orderMapper.sumByMap(map);

        if (turnover == null) {
            turnover = 0.0;
        }

        //平均客单价
        //已完成的总单数
        Integer statistics = orderMapper.countByMap(map);

        Double unitPrice = 0.0;

        if (statistics != 0) {
            unitPrice = turnover / statistics;
        }

        //有效订单数
        //已完成订单数
        Integer validOrderCount = orderMapper.countByMap(map);

        map.put("status", null);

        //订单总数
        Integer allOrderCount = orderMapper.countByMap(map);

        Double orderCompletionRate = 0.0;

        if (allOrderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / allOrderCount;
        }

        //新增用户数
        Double newUsers = userMapper.sumByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .unitPrice(unitPrice)
                .orderCompletionRate(orderCompletionRate)
                .validOrderCount(validOrderCount)
                .newUsers(newUsers.intValue())
                .build();

    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    public SetmealOverViewVO overviewSetmeals() {

        //已停售数量
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE);

        //已启售数量
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE);

        if (discontinued == null) {
            discontinued = 0;
        }

        if (sold == null) {
            sold = 0;
        }

        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();

    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    public SetmealOverViewVO overviewDishes() {

        //已停售数量
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);

        //已启售数量
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);

        if (discontinued == null) {
            discontinued = 0;
        }

        if (sold == null) {
            sold = 0;
        }

        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();

    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    public OrderOverViewVO overviewOrders() {

        Map map = new HashMap<>();
        map.put("beginTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        //待接单数量
        map.put("status", Orders.TO_BE_CONFIRMED);

        Integer waitingOrders = orderMapper.countByMap(map);

        //待派送数量
        map.put("status", Orders.CONFIRMED);

        Integer deliveredOrders = orderMapper.countByMap(map);

        //已完成数量
        map.put("status", Orders.COMPLETED);

        Integer completedOrders = orderMapper.countByMap(map);

        //已取消数量
        map.put("status", Orders.CANCELLED);

        Integer cancelledOrders = orderMapper.countByMap(map);

        //全部订单
        map.put("status", null);

        Integer allOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();

    }

    /**
     * 根据map动态查询运营数据
     *
     * @param begin
     * @param end
     * @return
     */
    public BusinessDataVO businessDatas(LocalDate begin, LocalDate end) {

        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        Map map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("status", Orders.COMPLETED);

        Double turnover = orderMapper.sumByMap(map);

        if (turnover == null) {
            turnover = 0.0;
        }

        //平均客单价
        //已完成的总单数
        Integer statistics = orderMapper.countByMap(map);

        Double unitPrice = 0.0;

        if (statistics != 0) {
            unitPrice = turnover / statistics;
        }

        //有效订单数
        //已完成订单数
        Integer validOrderCount = orderMapper.countByMap(map);

        map.put("status", null);

        //订单总数
        Integer allOrderCount = orderMapper.countByMap(map);

        Double orderCompletionRate = 0.0;

        if (allOrderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / allOrderCount;
        }

        //新增用户数
        Double newUsers = userMapper.sumByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .unitPrice(unitPrice)
                .orderCompletionRate(orderCompletionRate)
                .validOrderCount(validOrderCount)
                .newUsers(newUsers.intValue())
                .build();


    }

}
